package com.tanhua.manage.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.common.enums.UserStatusEnum;
import com.tanhua.common.mapper.UserFreezeMapper;
import com.tanhua.manage.mapper.UserStatusMapper;
import com.tanhua.manage.mapper.UserUnFreezeReasonMapper;
import com.tanhua.manage.pojo.UserStatus;
import com.tanhua.manage.pojo.UserUnfreezeReason;
import com.tanhua.manage.vo.UsersFreezeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserUnfreezeService {

    @Autowired
    private UserUnFreezeReasonMapper userUnFreezeReasonMapper;

    @Autowired
    private UserFreezeMapper userFreezeMapper;

    @Autowired
    private UserStatusMapper userStatusMapper;

    /**
     * @param userId            用户id
     * @param reasonsForThawing 解冻原因
     * @return
     */
    public UsersFreezeVo UserUnFreeze(Long userId, String reasonsForThawing) {

        //解冻之后往数据表里添加一条数据
        UserUnfreezeReason unFreezeReason = new UserUnfreezeReason(null, userId, reasonsForThawing);
        int insert = userUnFreezeReasonMapper.insert(unFreezeReason);

        //解冻之后删除user_freeze表中的数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        int delete = userFreezeMapper.delete(queryWrapper);

        //将状态表字段改为冻结
        UpdateWrapper<UserStatus> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        UserStatus userStatus = new UserStatus(null, userId, UserStatusEnum.pass);
        int update = userStatusMapper.update(userStatus, updateWrapper);

        //判断是否为空 为空就失败
        UsersFreezeVo usersFreezeVo = new UsersFreezeVo();
        if (ObjectUtil.isAllNotEmpty(insert, delete, update)) {
            String message = "操作成功";
            usersFreezeVo.setMessage(message);
            return usersFreezeVo;
        }
        return null;
    }
}
